<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Symbol-使用</title>
</head>
<body>
<script>
    let game = {
        up: function () {
            console.log('我能向上')
        },
        down: function () {
            console.log('我能向下')
        }
    }
    console.log(game)
    game.down()
    game.up()

    //使用Symbol向game对象中添加up和down方法，不污染原来game中的属性
    // 方法一：
    let method = {
        up: Symbol(),
        down: Symbol()
    };
    game[method.up] = function () {
        console.log('我可以改变形状')
    }
    game[method.down] = function () {
        console.log('我可以快速下落')
    }
    console.log(game)
    game[method.up]()
    game[method.down]()

    //方法二
    console.log('')
    console.log('方法二')
    console.log('')
    let game1 = {
        up: function () {
            console.log('我能向上')
        },
        down: function () {
            console.log('我能向下')
        },
        [Symbol('up')]: function () {
            console.log('我可以改变形状')
        },
        [Symbol('down')]: function () {
            console.log('我可以快速下落')
        }
    }
    console.log(game1)
    game1.up()
    game1.down()

    // 获取对象的所有 Symbol 属性
    let symbols = Object.getOwnPropertySymbols(game1);

    // 遍历 Symbol 属性并调用对应的方法
    symbols.forEach(item => {
        game1[item]();
    });

</script>
</body>
</html>